package net.ytbolg.mcxa.gui.Lang_and_ConfigManager;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import net.ytbolg.mcxa.gui.General;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Ytong
 */
public class Config {

    static JSONObject jo = null;
    static String str = "";
    static boolean isLoaded = false;

    public static Object getConfig(String key) {
        try {
            return jo.get(key);
        } catch (JSONException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public static String getConfigToString(String key) {
        if (!isLoaded) {
            return null;
        }
        try {
            return jo.getString(key);
        } catch (JSONException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;

    }

    public static void setConfig(String key, String value) {
        try {
            jo.put(key, value);
        } catch (JSONException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void LoadConfig(File f) {
        isLoaded = true;
        FileReader fr = null;
        try {
            fr = new FileReader(f);
            int c;
            while ((c = fr.read()) != -1) {
                str = str + (char) c;
            }
            General.Log(str, "Config");
            jo = new JSONObject(str);
            fr.close();
        } catch (IOException | JSONException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static void createConfig(File f, String Username, String memory, String pass) {
        try {
            JSONObject jobj = new JSONObject();
            jobj.put("LastLaunchVersionID", "-1");
            //    JSONObject jo2 = new JSONObject();
            jobj.put("UserName", Username);
            jobj.put("OnlineUse", !pass.equals("")?"true":"false");
            jobj.put("OnlinePassword", pass);
            //     jobj.put("UserInfo", jo2);
            jobj.put("LastMemory", memory);
            jobj.put("Language", "0");
            jobj.put("DownSrc", "0");
            jobj.put("SingleVersion", "false");
            jobj.put("MinecraftDir", General.AppDir + General.separator + ".minecraft");
            jobj.put("JavaPath", System.getProperty("java.home"));
            FileWriter fw = new FileWriter(f);
            fw.write(jobj.toString());
            fw.close();
        } catch (JSONException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
